package com.eqdd.yiqidian.ui.gongneng.renshi.quanxian;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.ThirdBean;
import com.eqdd.yiqidian.bean.rebuild.DepartmentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

/**
 * @author吕志豪 .
 * @date 17-11-14  上午10:22.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class DimissionLeaderSetActivity extends BaseActivity {

    private RecyclerViewCustom dataBinding;
    private SlimAdapterEx<ThirdBean> slimAdapterEx;
    private String msg;
    private EmploymentListResultBean bean;
    private ArrayList<DepartmentListResultBean> departs;
    private StringBuilder ids;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "设置部门离职审批人");
        initTopRightText("提交", v -> {
            if (checkParam()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_DIMISSION_LEADER)
                        .params("companyId", AppConstent.user.getCompanyId())
                        .params("userGUid", AppConstent.user.getGuid())
                        .params("cheker", bean.getGuid())
                        .params("departmentIds", ids.toString())
                        .execute(new DialogCallBack<HttpResult>(DimissionLeaderSetActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {

                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }


    private boolean checkParam() {
        if (TextUtils.isEmpty(slimAdapterEx.getDataItem(0).getThird())) {
            msg = "请选择离职审批人";
            return false;
        }
        if (TextUtils.isEmpty(slimAdapterEx.getDataItem(1).getThird())) {
            msg = "请选择部门";
            return false;
        }
        return true;
    }

    @Override
    public void initData() {

        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_08, new SlimInjector<ThirdBean>() {
            @Override
            public void onInject(ThirdBean data, IViewInjector injector) {

                injector.text(R.id.tv_upper_content, data.getFirst())
                        .hint(R.id.tv_down_content, data.getSecond())
                        .text(R.id.tv_down_content, data.getThird());
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    if (position == 0) {
                        Bundle bundle = new Bundle();
                        bundle.putString("companyId", AppConstent.user.getCompanyId() + "");
                        startActivityForResult(SelectSingleStaffActivity.class, bundle, Config.SELECT_SINGLE_STAFF);
                    } else if (position == 1) {
                        startActivityForResult(SelectBumenExpandActivity.class, null, Config.SELECT_BUMEN_EXPAND);
                    }
                });
    }

    @Override
    public void setView() {

        ArrayList<ThirdBean> data = new ArrayList<>();
        data.add(new ThirdBean("离职审批人", "请选择", ""));
        data.add(new ThirdBean("部门", "请选择", ""));
        slimAdapterEx.updateData(data);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.SELECT_SINGLE_STAFF && resultCode == Config.INTENT_CONTENT) {
            bean = (EmploymentListResultBean) data.getSerializableExtra("bean");
            slimAdapterEx.getDataItem(0).setThird(bean.getStaffName());
            slimAdapterEx.notifyItemChanged(0);
        } else if (requestCode == Config.SELECT_BUMEN_EXPAND && resultCode == Config.SUCCESS) {
            departs = data.getParcelableArrayListExtra("list");
            StringBuilder stringBuilder = new StringBuilder();
            ids = null;
            for (DepartmentListResultBean depart : departs) {
                stringBuilder.append(depart.getDepartName() + " ");
                if (ids == null) {
                    ids = new StringBuilder(depart.getDepartId());
                } else {
                    ids.append(",").append(depart.getDepartId());
                }
            }
            slimAdapterEx.getDataItem(1).setThird(stringBuilder.toString());
            slimAdapterEx.notifyItemChanged(1);
        }
    }
}
